//callmonst.txt - Basically functions like the default npc, except that, when damaged, 
//it calls for help. It sends message 53 to all creatures within 16 spaces. When they get 
//that message, they go active and immediately hunt down a random pc.
// Memory Cells:
//   Cell 0 - If 0, default behavior. If 1, it doesnt fidget.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, increment by 1.
//	Cell 3 - If left at 0, there is no animation. Otherwise, the sheet of the anim. to play
//  Cell 4 - Personality when talking. If 0, no talk.
begincreaturescript;

variables;

short i,target;
short broadcast = 0;
short last_call_check;
short alerted = 0;

body;

beginstate INIT_STATE;
	last_call_check = get_current_tick();
	set_aggression(ME,10);
	break;

beginstate DEAD_STATE;
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		inc_flag(get_memory_cell(1),get_memory_cell(2),1);
break;

beginstate START_STATE; 

	if ((my_current_message() == 53) && (target_ok() == FALSE)) {
		alerted = 1;
		set_act_at_dist(ME,1);
		if (get_ran(1,0,100) < 40)
			set_foe_target(ME,random_party_member());
		}
	if ((alerted > 0) && (get_ran(1,0,100) < 30) && (target_ok() == FALSE)) {
		set_foe_target(ME,random_party_member());
		set_state(3);
		}
		
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		approach_char(ME,get_target(),6);
		set_state(3);		
		}
	
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	
	
	if (my_dist_from_start() >= 6) {
		if (get_ran(1,1,100) < 40) 
			return_to_start(ME,1);
		}
		else if (get_memory_cell(0) == 0)
			fidget(ME,20);

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking


	if (last_call_check != get_current_tick()) {
		last_call_check = get_current_tick();
		if ((broadcast == 0) && (get_ran(1,0,100) < 30)) {
			if (can_see_char(1000))
				print_named_str(ME,"calls for help!");
			broadcast = 1;
			broadcast_char_message(13,53,1);
			if (get_memory_cell(3) > 0)
				run_char_animation(get_memory_cell(3),TRUE,75);
				else run_char_animation(2,1,35);	
			pc_heard_sound(169);
			end();
			}
		}
		
	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(4) == 0) {
		print_str("Talking: It doesn't respond.");
		}
		else begin_talk_mode(get_memory_cell(4));
		
break;